<template>
    <el-card class="!border-none " shadow="never">
        <el-form class="-mb-4 " :model="queryFormData" label-width="98px" inline>
            <div class="grid grid-cols-4 gap-4">
                <div>
                    <el-form-item label="物流单号">
                        <el-input
                            placeholder="请输入物流单号"
                            
                            class="w-64"
                            v-model="queryFormData.logisticNumber"
                            clearable
                            @keyup.enter="resetOnePage"
                        >
                        
                        </el-input>
                    </el-form-item>
                </div>
                
                <div>
                    <el-form-item label="工单号">
                        <el-input
                            placeholder="请输入工单号"
                            
                            class="w-64"
                            v-model="queryFormData.afterSaleOrderNo"
                            clearable
                            @keyup.enter="resetOnePage"
                        >
                        
                        </el-input>
                    </el-form-item>
                </div>
                
                
                <div>
                    <el-form-item label="交易订单号">
                        <el-input
                            class="w-64"
                            placeholder="请输入用户交易订单号"
                            
                            v-model="queryFormData.orderNumber"
                            clearable
                            @keyup.enter="resetOnePage"
                        >
                        </el-input>
                    </el-form-item>
                </div>
                
                <div v-if="!showMore">
                    <el-form-item label="协同类型" prop="cooperateOrderType">
                        
                        <el-select class="w-64" placeholder="请输入工单类型" v-model="queryFormData.cooperateOrderType"
                                   @keyup.enter="resetOnePage">
                            <el-option
                                v-for="item in afterSaleOrderConfig.cooperateOrderType"
                                :label="item.name"
                                :value="item.code"
                            >
                            
                            </el-option>
                        </el-select>
                    
                    </el-form-item>
                </div>
                
                <div v-if="!showMore">
                    <el-form-item label="用户联系方式">
                        <el-input
                            class="w-64"
                            placeholder="请输入用户联系方式"
                            
                            v-model="queryFormData.userPhone"
                            clearable
                            @keyup.enter="resetOnePage"
                        >
                        </el-input>
                    </el-form-item>
                </div>
                
                <div v-if="!showMore">
                    <el-form-item label="店铺名称">
                        
                        <el-select class="w-64" v-model="queryFormData.shopName" placeholder="请输入店铺名称"
                                   @keyup.enter="resetOnePage"
                        >
                            <el-option v-for="item in shopSelect"
                                       :label="item.value"
                                       :value="item.name"
                                       @keyup.enter="resetOnePage"/>
                        </el-select>
                    
                    </el-form-item>
                </div>
                
                <!--                做下拉-->
                <div v-if="!showMore">
                    <el-form-item label="工单来源" prop="source">
                        
                        
                        <el-select class="w-64" v-model="queryFormData.source" placeholder="请输入工单来源"
                                   @keyup.enter="resetOnePage">
                            <el-option v-for="item in afterSaleOrderConfig.orderSourceConfig"
                                       :label="item.value"
                                       :value="item.name"
                                       @keyup.enter="resetOnePage"/>
                        </el-select>
                    
                    
                    </el-form-item>
                </div>
                
                
                <div class="flex flex-row-reverse">
                    <el-form-item>
                        <div class="flex-1  flex flex-row-reverse  w-full ">
                            <div class="mx-2 justify-center flex-row  flex items-center">
                                <el-dropdown v-if="showMore">
                              <span @click="putMenu">
                                    展开
                                 <icon name="el-icon-ArrowDown"/>
                              </span>
                                </el-dropdown>
                                
                                <el-dropdown v-if="showMore!==true">
                              <span @click="putMenu">
                                    收起
                                 <icon name="el-icon-ArrowUp"/>
                              </span>
                                </el-dropdown>
                            
                            </div>
                            
                            
                            <el-button class="mx-2" type="primary" @click="getPageLists">查询</el-button>
                            <el-button class="mx-2" @click="resetParams">重置</el-button>
                        
                        </div>
                    
                    </el-form-item>
                </div>
            
            </div>
        </el-form>
    </el-card>
    
    
    <el-card class="!border-none mt-5" shadow="never">
        <div class="mt-4 flex justify-end">
            <el-tag
                class="mx-2 hover:text-danger hover:bg-warning   cursor-pointer"
                @click="getCurrentStatus(null)"
                effect="light"
                round
                type="info">
                全部
            </el-tag>
            
            <el-tag v-for="(item,index) in afterSaleOrderConfig.cooperateOrderStatus"
                    class="mx-2 hover:text-danger hover:bg-warning   cursor-pointer"
                    @click="getCurrentStatus(item.code)"
                    effect="light"
                    :key="index"
                    round
                    type="info">
                {{ item.name }}
            </el-tag>
        </div>
        
        <el-table
            ref="tableRef"
            class="mt-4 divide-x-4 "
            size="large"
            v-loading="pagerData.loading"
            :data="pagerData.lists"
            row-key="id"
        >
            <el-table-column label="订单号" fixed prop="afterSaleOrderDetail.orderNumber" :show-overflow-tooltip="true"
                             min-width="200"/>
            <el-table-column label="工单号" prop="cooperateOrderNo" min-width="190"/>
            
            
            <el-table-column label="类型" prop="type" :formatter="formatterManager.formatterCooperateType"
                             min-width="150">
            
            </el-table-column>
            <el-table-column label="协同状态" prop="status" min-width="120">
                <template #default="{ row }">
                    
                    <el-tag v-for="(item,index) in afterSaleOrderConfig.cooperateOrderStatus"
                            v-show="row.status === item.code"
                            :key="index"
                            :disable-transitions="true"
                            :type="item.type">
                        {{ item.name }}
                    </el-tag>
                </template>
            </el-table-column>
            
            
            <el-table-column label="受理人" prop="acceptPerson"
                             min-width="120"></el-table-column>
            
            <el-table-column label="介入处理人" prop="intervenePerson" :show-overflow-tooltip="true"
                             min-width="120"></el-table-column>
            
            <el-table-column label="渠道" prop="afterSaleOrderDetail.channel" min-width="120"
                             :formatter="formatterManager.formatterChannel"></el-table-column>
            
            
            <el-table-column label="店铺" prop="afterSaleOrderDetail.shopName" min-width="120"
                             :formatter="formatterManager.formatterShop"></el-table-column>
            
            
            <el-table-column label="用户名称" prop="afterSaleOrderDetail.userName" min-width="120"/>
            <el-table-column label="用户电话" prop="afterSaleOrderDetail.userPhone" min-width="150"/>
            
            
            <el-table-column label="建单时间" prop="createTime " min-width="180"
                             :formatter="formatterManager.formatterCreateTime"/>
            
            <el-table-column label="提交人" prop="createPerson"
                             min-width="120"></el-table-column>
            
            <el-table-column label="操作" width="170" fixed="right">
                <template #default="{ row }">
                    <el-button
                        type="primary"
                        link
                        @click="handleRowDetail(row)"
                    >
                        详情
                    </el-button>
                
                
                </template>
            </el-table-column>
        </el-table>
        
        <pagination v-model="pagerData" @change="getPageLists"/>
    
    </el-card>
    
    
    <Edit ref="editRef" @close="closeEditDetail" v-if="showEditRef"/>

</template>

<script lang='ts' setup>


import {usePagerManager} from "@/manager/base/PagerManager";

import {getCooperateListByPage} from "@/api/afterSale/order/AfterSaleOrderListController";
import {UnwrapNestedRefs} from "@vue/reactivity";
import {useFormatterManager} from "@/manager/base/FormatterManager";
import {useAfterSaleEnumManager} from "@/manager/base/AfterSaleEnumManager";
import cooperateOrderSearchRequest, {
    CooperateOrderSearchRequest
} from "@/api/afterSale/cooperate/request/CooperateOrderSearchRequest";
import Edit from './edit.vue'
import {CooperateOrderDetailDTO} from "@/api/afterSale/cooperate/dto/CooperateOrderDetailDTO";

let afterSaleOrderConfig = useAfterSaleEnumManager();
let formatterManager = useFormatterManager(afterSaleOrderConfig);


const editRef = shallowRef(Edit)
const showMore = ref(true)
const showEditRef = ref(false)


/**
 * 定义查询数据
 */
let queryFormData: UnwrapNestedRefs<CooperateOrderSearchRequest> = reactive(cooperateOrderSearchRequest())

const shopSelect = computed(() => {
    
    let arr: any[] = [];
    afterSaleOrderConfig.orderChannelConfig.filter(item => item.children).forEach(item => {
        arr.push(...item.children)
    })
    return arr
})
/**
 * 获取分页数据
 */
const {pagerData, getPageLists, resetParams, resetOnePage} = usePagerManager({
    fetchFun: getCooperateListByPage,
    params: queryFormData
})


/**
 *  收起展开菜单
 *
 * @author 徐寿春
 * 2023/4/6 13:44
 */
function putMenu() {
    showMore.value = !showMore.value
}


/**
 *  获取当前状态 -- 点击
 *
 * @author 徐寿春
 * 2023/4/6 13:44
 */
function getCurrentStatus(row: any) {
    queryFormData.status = row
    getPageLists()
}


// ------------------------------------------ ** 处理详情 ** ------------------------------------------------------------------------------------

/**
 * 详情页面关闭
 *
 * @author 徐寿春
 * 2023/4/17 10:37
 */
function closeEditDetail() {
    showEditRef.value = false
    getPageLists()
    
}


/**
 *  处理详情页
 *
 * @author 徐寿春
 * 2023/4/6 13:45
 */
async function handleRowDetail(row: CooperateOrderDetailDTO) {
    showEditRef.value = true
    await nextTick()
    editRef.value?.open(row.cooperateOrderId)
}


/**
 * 获取工单列表
 */
onMounted(() => {
    getPageLists()
})


</script>

